package com.studymaster.controller;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJacksonJsonView;
import com.studymaster.domain.User;
import com.studymaster.service.UserService;


@Controller
public class LoginController {
   
	@Autowired
	private UserService userService;
	
	@RequestMapping(value = "/login.do" , method = RequestMethod.POST)	
	public ModelAndView login(@RequestParam("studentid") String studentid,
			@RequestParam("password") String password,HttpServletRequest request
			, ModelMap model){
		//ModelMap model = new ModelMap();// 初始化数据模型对象model
		User loginUser = userService.getUserById(studentid);
		if (loginUser == null) {
			model.put("errorcode", "1");// 用户不存在;
		} else if (!loginUser.getPassword().equals(password)) {
			model.put("errorcode", "2");// 密码错误
		}else {
			model.put("errorcode", 0);
			model.put("studentid", loginUser.getStudentid());
			model.put("password", loginUser.getPassword());
			model.put("roleid", loginUser.getRoleid());
			model.put("rolename", loginUser.getRolename());
		}
		return new ModelAndView(new MappingJacksonJsonView(), model);
		
	}
}
